<?php



namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Area;
use app\common\model\Version;
use fast\Random;
use think\captcha\Captcha;
use think\Config;
use think\Db;
use think\Hook;

/**
 * 公共接口
 */
class Client extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';



    /* 信息列表 */
    public function getmyclient(){

        $user=$this->auth->getUser();

        $map['quyu_id']=['eq',$user['quyu_id']];

        $key=$this->request->param('keys');

        if ($key){
            $map['name|c_mobile']=['like','%'.$key.'%'];
        }

        $types=$this->request->param('types');

        if ($types==1){
            $map['types']=['eq',1];
        }else if($types==2){
            $map['types']=['eq',0];
        }else if($types==3){
            $map['ctypes']=['eq',1];
        }else if($types==4){
            $map['ctypes']=['eq',2];
        }else if($types==5){
            $map['ctypes']=['eq',3];
        }



        $list=Db::name('aa_client')->where($map)->order('id desc')->select();

//        $ret=Db::getLastSql();
//        echo $ret;


        $str=strtotime(date('Y-m'));

        foreach ($list as &$ke){


            $ke['time']=date('Y-m-d H:i',$ke['time']);
            $ke['ing']=getjisuan($ke);

            $count=Db::name('client_log')
                ->where('time','gt',$str)
                ->where('aa_client_id','eq',$ke['id'])
                ->count();


            $ke['see']=$count;
        }

        $this->success('信息列表',$list);

    }

    /*信息详情*/
    public function getinfobyid(){

        $id=$this->request->param('id');

        $info=Db::name('aa_client')->where(['id'=>$id])->find();

        if($info['wimages']){
            $info['wimages']=explode(',',$info['wimages']);
        }else{
            $info['wimages']=[];
        }


        foreach ($info as $key=>$v){
            $info[$key]=$v==null?'':$v;
        }

        $cytpes='掉客';



        if ($info['ctypes']=='1'){
            $cytpes='合作客户';
        }else if($info['ctypes']=='2'){
            $cytpes='待合作客户';
        }else{
            $cytpes='掉客';
        }


        $info['ctypes_t']=$cytpes;

        $this->success('信息列表',$info);

    }
        /*
         * 提交访问信息
         * */
    public function informationclient(){

        $user=$this->auth->getUser();

        $user=Db::name('user')->where(['id'=>$user['id']])->find();

        $title=$this->request->param('array_t');
        $desc=$this->request->param('desc');
        $id=$this->request->param('id');


        $latitude=$this->request->param('latitude');
        $longitude=$this->request->param('longitude');

        $client=Db::name('aa_client')->where(['id'=>$id])->find();

        if (!$longitude){
            $this->error('无坐标信息');
        }

        if(!$client['lat']){

            Db::name('aa_client')->where(['id'=>$id])->update(['lat'=>$latitude,'lng'=>$longitude]);

            $client['lat']=$latitude;
            $client['lng']=$longitude;


        }

        // 计算距离
        $distance = haversineGreatCircleDistance($latitude, $longitude, $client['lat'], $client['lng']);

        //距离超过200
       if (intval($distance)>300){
            $this->error('距离过远'.intval($distance).'-'.$latitude.'-'.$longitude.'-'.$client['lat'].'-'. $client['lng']);
       }

        if (!$title||!$desc){
            $this->error('请填写完整信息！','');
        }

        $log['user_id']=$user['id'];
        $log['aa_client_id']=$id;


        $info=Db::name('client_log')->where($log)->order('id desc')->find();

        if ($info&&$info['time']>(time()-60)){

            $this->error('请不要频繁提交！','');
        }


        $log['desc']=$desc;
        $log['title']=$title;

        $log['time']=time();
        $log['types']=$client['types'];
        $log['quyu_id']=$user['quyu_id'];



        Db::name('client_log')->data($log)->insert();

        $this->success('操作成功！！','');

    }


    /*
     * 我的访问记录 取最近30个
     * */

    public function getseelogbyid(){

        $user=$this->auth->getUser();




        $userinfo=Db::name('user')->where(['id'=>$user['id']])->find();


        $lists=Db::name('client_log')
            ->where(['user_id'=>$user['id'],'quyu_id'=>$userinfo['quyu_id']])

            ->limit(30)
            ->order('id desc')
            ->select();

        foreach ($lists as &$key){

            $client=Db::name('aa_client')->where(['id'=>$key['aa_client_id']])->find();

            $key['time']=date('Y-m-d H:i');
            $key['cname']=$client['name'];
        }

//        Db::name()->where()->update();

        $this->success('我的访问记录',$lists);

    }

    /*
   * 我的访问记录 取最近30个
   * */

    public function getseelogbyidone(){

        $user=$this->auth->getUser();


        $id=$this->request->param('id');

        $userinfo=Db::name('user')->where(['id'=>$user['id']])->find();


        $lists=Db::name('client_log')
            ->where(['user_id'=>$user['id'],'quyu_id'=>$userinfo['quyu_id']])
            ->where('aa_client_id','eq',$id)
            ->limit(30)
            ->order('id desc')
            ->select();

        foreach ($lists as &$key){

            $client=Db::name('aa_client')->where(['id'=>$key['aa_client_id']])->find();

            $key['time']=date('Y-m-d H:i',$key['time']);
            $key['cname']=$client['name'];
        }


        $this->success('我的访问记录',$lists);

    }

    /* 修改信息 */
    public function submits()
    {

        $info=$this->request->param();

//        if (!$info['name']||!$info['address']||!$info['image']||!$info['wimages']){
//            $this->error('请填写完整信息！');
//        }

        $info['wimages']=implode(",",$info['wimages']);

        $id=$info['id'];

        unset($info['id']);


        $info['c_sex']=$info['c_sex']?'1':'0';
        $info['m_sex']=$info['m_sex']?'1':'0';


        Db::name('aa_client')->where(['id'=>$id])->update($info);

        $this->success('修改成功',$info);


    }


    /*
   * 提交信息
   * */
    public function submit()
    {

        $info=$this->request->param();

        if (!$info['name']){
            $this->error('请填写名称！');
        }
        if (!$info['address']){
            $this->error('请填写地址！');
        }
        if (!$info['image']){
            $this->error('请填写门头照片！');
        }


        $log=Db::name('aa_client')->where(['name'=>$info['name']])->find();

        if ($log){
            $this->error('名称存在！');
        }


        $info['wimages']=implode(",",$info['wimages']);

        $user=$this->auth->getUser();

        $userinfo= Db::name('user')->where(['id'=>$user['id']])->find();


        $info['quyu_id']=$userinfo['quyu_id'];
        $info['c_sex']=$info['c_sex']?'1':'0';
        $info['m_sex']=$info['m_sex']?'1':'0';

        $info['seetime']=date('md');
        $info['ctypes']=$info['ctypes']+1;

        $info['time']=time();
        $info['ctypestime']=time();

        $cid=Db::name('aa_client')->insertGetId($info);




        //第一次的时候  添加一个膜拜记录
        $log['user_id']=$userinfo['id'];
        $log['desc']='首次膜拜';
        $log['title']='膜拜';
        $log['aa_client_id']=$cid;
        $log['time']=time();
        $log['types']=0;
        $log['quyu_id']=$userinfo['quyu_id'];

        Db::name('client_log')->data($log)->insert();


        $this->success('提交成功',$info);


    }

/*
 * 对手信息
 * */

    public function getallrival(){


        $lists= Db::name('aa_rival')->select();

        $this->success('对手信息',$lists);


    }


    /* 当日信息 */

    public function getmyclients(){

        $id=$this->request->param('id');


        $user=$this->auth->getUser();

        $userinfo= Db::name('user')->where(['id'=>$user['id']])->find();


        $list=[];

        if ($id==1){

            $list=Db::name('aa_client')->alias('a')
                ->join('fa_client_log b','a.id=b.aa_client_id')
                ->where('b.time','gt',strtotime(date('Y-m-d')))
                ->where('b.quyu_id','eq',$userinfo['quyu_id'])

                ->field('a.*')
                ->order('id desc')->select();

        }else if($id==2){
            $list=Db::name('aa_client')
                ->where('time','gt',strtotime(date('Y-m-d')))
                ->where('quyu_id','eq',$userinfo['quyu_id'])
                ->order('id desc')->select();
        }else if($id==3){

            $list=Db::name('aa_client')
                ->where('ctypes','eq',1)
//                ->where('time','gt',strtotime(date('Y-m-d')))
                ->where('quyu_id','eq',$userinfo['quyu_id'])
                ->order('id desc')->select();


        }else if($id==4){
            $list=Db::name('aa_client')
                ->where('ctypes','eq',2)
                ->where('time','gt',strtotime(date('Y-m-d')))
                ->where('quyu_id','eq',$userinfo['quyu_id'])
                ->order('id desc')->select();
        }else if($id==5){
            $list=Db::name('aa_client')
                ->where('ctypes','eq',3)
                ->where('time','gt',strtotime(date('Y-m-d')))
                ->where('quyu_id','eq',$userinfo['quyu_id'])
                ->order('id desc')->select();
        }


//        $ret=Db::getLastSql();
//        echo $ret;

        foreach ($list as &$ke){
            $ke['time']=date('Y-m-d H:i',$ke['time']);
            $ke['ing']=getjisuan($ke);
        }

        $this->success('信息列表',$list);




    }







}